
function XmlDocument()
{
	
	var active = ["MSXML4.DOMDocument","MSXML3.DOMDocument",
	                  "MSXML2.DOMDocument","MSXML.DOMDocument",
	                  "Microsoft.XmlDom"];
	try{
	    this.domParser = new DOMParser();
	    
	    this.xmlDoc = this.domParser.parseFromString("<root/>",'text/xml');
	   
	 }
	catch(e)
	{
		
		for(i=0; i<active.length; i++)
		  {
              try{
            	  
            	  this.xmlDoc = new ActiveXObject(active[i]);
                  break;
              }catch(e){}
          }
	}
	
	if (arguments.length > 0)
	{
		this.xmlDoc.loadXML(arguments[0]);
	}	
	
	this.actvieNode = null;
	

	XmlDocument.prototype.loadXML       = XmlDocument_loadXML;	

	XmlDocument.prototype.createNode    = XmlDocument_createNode;	
 
	XmlDocument.prototype.appendNode    = XmlDocument_appendNode;	
	
	XmlDocument.prototype.appendCDATA    = XmlDocument_appendCDATA;	

	XmlDocument.prototype.createAttribute = XmlDocument_createAttribute;	
 
	XmlDocument.prototype.appendAttribute = XmlDocument_appendAttribute;			

	XmlDocument.prototype.getXml           = XmlDocument_getDocXml;		

	XmlDocument.prototype.getRootNode      = XmlDocument_getRootNode;	

	XmlDocument.prototype.getAttributeText = XmlDocument_getAttributeText;

	XmlDocument.prototype.getNodeText      = XmlDocument_getNodeText;

	XmlDocument.prototype.getNodeXml       = XmlDocument_getNodeXml;	

	XmlDocument.prototype.free             = XmlDocument_free;	
	
}

function XmlDocument_loadXML(xmlStr){
	
	if(window.isIE11){
		
		if(this.xmlDoc.loadXML == undefined){
			this.xmlDoc = this.domParser.parseFromString(xmlStr,'text/xml');			
		}else{
			this.xmlDoc.loadXML(xmlStr);
		}

	}else{
		
		if (!window.isIE)
			this.xmlDoc = this.domParser.parseFromString(xmlStr,'text/xml');
		
		else{
			
			if(this.xmlDoc == undefined){
				this.xmlDoc = this.domParser.parseFromString(xmlStr,'text/xml');			
			}else{
				if(this.xmlDoc.loadXML){
					this.xmlDoc.loadXML(xmlStr);
				}	
			}
			
			/*if(xmlDoc == undefined){}
			else{this.xmlDoc.loadXML(xmlStr);}*/
		}
		
	}
	
/*	if (!window.isIE)
		this.xmlDoc = this.domParser.parseFromString(xmlStr,'text/xml');
	else
		this.xmlDoc.loadXML(xmlStr);*/
	
	this.getRootNode();
}

function XmlDocument_createNode(nodeName)
{

	var nodeText = "";

	if (arguments.length > 1)
	{
		nodeText = arguments[1];
	}
	this.actvieNode = this.xmlDoc.createElement(nodeName);

	if (nodeText.toString().length > 0)
	{
		var Textnode=xmlDoc.createTextNode(nodeText);
		this.actvieNode.appendChild(Textnode);
	}	
	return this.actvieNode;
}

function XmlDocument_appendNode(parentNode,nodeName)
{	
	
	if (arguments.length ==2)
	{
		this.actvieNode = this.createNode(nodeName);
		
	}else if (arguments.length > 2)
	{
		this.actvieNode = this.createNode(nodeName,arguments[2]);
	}	
	if(this.actvieNode ==null){
		alert("生成子节节点失败！");
		return;	
	}
		
	parentNode.appendChild(this.actvieNode);	
	
	return this.actvieNode;
}

function XmlDocument_createAttribute(attrName)
{

	var nodeText = "";
	if (arguments.length > 1)
	{
		nodeText = arguments[1];
	}
	var attr = this.xmlDoc.createAttribute(attrName);
	if (nodeText != "")
	{
		attr.value = nodeText;
	}
	return attr;
}

function XmlDocument_appendAttribute(attrName)
{
	var nodeAttr= null;
	if (arguments.length ==1)
	{
		nodeAttr = this.createAttribute(attrName);
	}else if (arguments.length >1)
	{
		nodeAttr = this.createAttribute(attrName,arguments[1]);
	}
		
	if(nodeAttr ==null){
		alert("生成属性失败！");
		return;	
	}
	if (this.actvieNode)
		this.actvieNode.attributes.setNamedItem(nodeAttr);
	
	return nodeAttr;
}

function XmlDocument_appendCDATA(dataSection)
{
	var CDATASection = this.xmlDoc.createCDATASection(dataSection);
	if (this.actvieNode)
		this.actvieNode.appendChild(CDATASection);
	return CDATASection;
}

function XmlDocument_getAttributeText(attrName)
{
	
	try{
		if (this.actvieNode)
			var attrText = this.actvieNode.getAttribute(attrName);
		
		if (!attrText)
			attrText = "";
	}catch(e)
	{
		processException(e);
	}
		
	return attrText;
}

function XmlDocument_getNodeText(node)
{
	var nodeText="";
	
	if(node == undefined)
	{
		if (this.actvieNode)
			nodeText = this.actvieNode.text;
	}else
	{
		nodeText = node.text;
	}
	if (!nodeText)
	{
		nodeText = "";
	}	
	return nodeText;
}

function XmlDocument_getNodeXml(node){
	
	var nodeXml = "";
	
	if(node == undefined)
	{
		nodeXml = this.getRootNode().xml;
	}else
	{
		if (this.actvieNode)
			nodeXml = this.actvieNode.xml;
	}
	if (!nodeXml)
	{
		nodeXml = "";
	}	
	return nodeXml;
}

function XmlDocument_getDocXml(node)
{
	if (arguments.length == 0)
		node = this.getRootNode();
	
	var xmlStr ="";
	
	var serial = new XMLSerializer();
	
	if(serial && serial.serializeToString){
		xmlStr = (new XMLSerializer()).serializeToString(node);		
	}
		
	if(xmlStr == undefined){
		xmlStr = node.xml;
	}
	
/*	if (!window.isIE)
		xmlStr =(new XMLSerializer()).serializeToString(node);
	else
		xmlStr = node.xml;
	
	if(window.isIE11){
		xmlStr = (new XMLSerializer()).serializeToString(node);
		
		if(xmlStr == undefined){
			xmlStr = node.xml;
		}
	}*/
	
/*	if (!window.isIE)
		xmlStr =(new XMLSerializer()).serializeToString(node);
	else
		xmlStr = node.xml;*/
	
	return xmlStr;
}

function XmlDocument_getRootNode(){
	this.actvieNode = this.xmlDoc.documentElement;
	return this.actvieNode;
}

function XmlDocument_free(){
	
	this.xmlDoc = null;
	delete this.xmlDoc;
}

